package com.masterclient.app.httpservice;

import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.eoner.managerlibrary.netsign.version1.GetCookiesInterceptor;
import com.eoner.managerlibrary.netsign.version1.HttpInterceptor;
import com.eoner.managerlibrary.netsign.version1.SignInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者：sean
 * 创建时间：2017/6/9 14:31
 * 功能说明：获取retrofit实例
 */

public class RetrofitUtil {
//    private static Retrofit ourInstance;
    private static Api api;

    public static Api getInstance() {
        if(api == null){
            synchronized (RetrofitUtil.class){
                if(api == null){
                    new RetrofitUtil();
                }
            }
        }
        return api;
    }

    private RetrofitUtil() {
        //声明日志类
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        //设定日志级别
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//NONE
        //自定义OkHttpClient
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
        okHttpClient.connectTimeout(30, TimeUnit.SECONDS);
        okHttpClient.readTimeout(30,TimeUnit.SECONDS);
        //添加拦截器
        okHttpClient.addInterceptor(new GetCookiesInterceptor());
        //添加签名
        okHttpClient.addInterceptor(new HttpInterceptor());
        //添加请求头
        okHttpClient.addInterceptor(new SignInterceptor());
        okHttpClient.addInterceptor(httpLoggingInterceptor);
//      okHttpClient.addInterceptor(new NewHttpInterceptor());
        Retrofit ourInstance = new Retrofit.Builder()
                .baseUrl(NetEnvManager.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).client(okHttpClient.build()).build();
        api = ourInstance.create(Api.class);
    }
}